﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Xml;

namespace LPT_5.Twitter
{
    class TwitClient
    {
        private const String TWITTER_IP = "128.121.146.228";
        private const int TWITTER_PORT = 80;

        private HTTPSocket.Socket hsAPISock = null;

        public TwitClient(String sUsername, String sPassword)
        {
            String sConcat = sUsername + ":" + sPassword;
            String sAuthString = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(sConcat));

            hsAPISock = new LPT_5.HTTPSocket.Socket(TWITTER_IP, TWITTER_PORT);

            //Twitter uses HTTP Basic Auth, which is sent in the headers
            hsAPISock.AddHeader("Authorization", "Basic " + sAuthString);
            hsAPISock.AddHeader("User-Agent", "TwitClient/C#");
        }

        public XmlDocument GetFriendsTimeline(int iCount)
        {
            String sResult = DoAPIRequest("/statuses/friends_timeline.xml?count="+iCount);

            XmlDocument xdResult = new XmlDocument();
            xdResult.LoadXml(sResult);
            return xdResult;
        }

        private String DoAPIRequest(String sAPIReq)
        {
            return hsAPISock.GetURL(sAPIReq);
        }
    }
}